// 类型约束
interface Iutils {
  toCelsius: (tem: number) => number;
  toFahrenheit: (tem: number) => number;
  [k: string]: any
}
let utils: Iutils = {
  toCelsius(tem) {
    return ((tem - 32) * 5) / 9;
  },
  toFahrenheit(tem) {
    return (tem * 9) / 5 + 32;
  },
};

/**
 * 
 * @param temperature 传入的温度
 * @param convert 转换函数的名字
 * @returns 返回计算的结果
 */

//类型
type ItryConvert = (temperatrue: string, convert: string) => string
let tryConvert: ItryConvert
// 重写window中的isNaN类型
type IisNaN = (param: any) => boolean
let isNaN2: IisNaN = window.isNaN
tryConvert = function (temperature, convert) {
  if (isNaN2(temperature) || temperature == "") {
    return "";
  }
  const input = parseFloat(temperature);

  const output = utils[convert](input);
  const rounded = Math.round(output * 1000) / 1000;
  return rounded.toString();
}

export default tryConvert